import { BitMath } from "../../math/math";
import { App_Interface } from "../app_interface";
import { Tile } from "../../tile/tile";
import { Adapter } from "../../adapter/adapter";

export namespace GridElement
{
    export class Canvas implements App_Interface.IGridElementGroup
    {
        get IsGroup(): boolean 
        {
            return true;
        }
        children: App_Interface.IGridElement[] = [];
        AddChild(element: App_Interface.IGridElement): void
        {
            if (this.children.includes(element))
                return;

            if (element.Parent != null)
                element.Parent.RemoveChild(element);
            this.children.push(element);

            element.SetParent(this);

            if (element.IsGroup)
                (element as App_Interface.IGridElementGroup).OnParentResize(this.TileRectLocal.w, this.TileRectLocal.h);

        }
        RemoveChild(element: App_Interface.IGridElement): void
        {
            let i = this.children.indexOf(element);
            if (i >= 0)
            {
                this.children.splice(i, 1);
                element.SetParent(null);
            }
        }
        OnParentResize(tileWidth: number, tileHeight: number): void
        {
            //do sth here

            for (var i = 0; i < this.children.length; i++)
            {
                if (this.children[i].IsGroup)
                {
                    let group =this.children[i] as App_Interface.IGridElementGroup;
                    group.OnParentResize(this.TileRectLocal.w,this.TileRectLocal.h);
                }
            }
        }


        get TileXGlobal(): number
        {
            if (this.Parent == null)
                return this.TileRectLocal.x;
            else
                return this.TileRectLocal.x + this.Parent.TileXGlobal;
        }
        get TileYGlobal(): number
        {
            if (this.Parent == null)
                return this.TileRectLocal.y;
            else
                return this.TileRectLocal.y + this.Parent.TileYGlobal;
        }

        TileRectLocal: BitMath.Rect =
            new BitMath.Rect(0, 0, 1, 1);


        border: Tile.IDrawElement_WithSize | null = null;
        borderMultiColorIndex: number = 0;

        private static _RenderRect: BitMath.Rect = new BitMath.Rect(0, 0, 8, 8);
        OnDraw(render: Adapter.IRenderAdapter): void
        {
            for (var i = 0; i < this.children.length; i++)
            {
                this.children[i].OnDraw(render);
            }
            //draw border
            if (this.border != null)
            {
                Canvas._RenderRect.x = this.TileXGlobal * 8;
                Canvas._RenderRect.y = this.TileYGlobal * 8;
                Canvas._RenderRect.w = this.TileRectLocal.w * 8;
                Canvas._RenderRect.h = this.TileRectLocal.h * 8;
                this.border.DrawScale(render, Canvas._RenderRect, this.borderMultiColorIndex);
            }
        }
        Parent: App_Interface.IGridElementGroup | null = null;
        SetParent(parent: App_Interface.IGridElementGroup): void
        {
            this.Parent = parent;
        }

    }
}
